package com.example.myretrofit2.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.myretrofit2.Goods;
import com.example.myretrofit2.R;
import com.example.myretrofit2.activity.LoginActivity;
import com.example.myretrofit2.common.MyApplication;
import com.example.myretrofit2.presenter.MyPresenter;
import com.example.myretrofit2.presenter.MySubscriber;


public class NavigationFragment extends Fragment implements View.OnClickListener {
    FragmentManager fm;
    ImageButton tabItemHomeBtn;//home的按钮
    ImageButton tabItemCategoryBtn;
    ImageButton tabItemCarBtn;
    ImageButton tabItemPersonalBtn;
    LinearLayout tabItemHome;
    LinearLayout tabItemCategory;
    LinearLayout tabItemCar;
    LinearLayout tabItemPersonal;
    HomeFragment homeFragment = null;
    CategoryFragment categoryFragment = null;
    CarFragment carFragment = null;
    PersonalFragment personalFragment = null;
    String car;
    public NavigationFragment(){

    }
    public NavigationFragment(String car){
        this.car = car;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_navigation, container, false);
        initView(view);
        if("car".equals(car)){
            tabBtn(R.id.tab_item_car);
        }else {
            tabBtn(R.id.tab_item_home);
        }
        return view;
    }

    /**
     * 初始化控件
     * @param view
     */
    private void initView(View view){
        tabItemHomeBtn = view.findViewById(R.id.tab_item_home_btn);
        tabItemCarBtn = view.findViewById(R.id.tab_item_cart_btn);
        tabItemCategoryBtn = view.findViewById(R.id.tab_item_category_btn);
        tabItemPersonalBtn = view.findViewById(R.id.tab_item_personal_btn);
        tabItemHome = view.findViewById(R.id.tab_item_home);
        tabItemCar = view.findViewById(R.id.tab_item_car);
        tabItemCategory = view.findViewById(R.id.tab_item_category);
        tabItemPersonal = view.findViewById(R.id.tab_item_personal);
        tabItemHome.setOnClickListener(this);
        tabItemCar.setOnClickListener(this);
        tabItemCategory.setOnClickListener(this);
        tabItemPersonal.setOnClickListener(this);
        fm = getActivity().getSupportFragmentManager();

    }
    private void tabBtn(int id){
        tabItemHomeBtn.setImageResource(R.drawable.tab_item_home_normal);
        tabItemCategoryBtn.setImageResource(R.drawable.tab_item_category_normal);
        tabItemCarBtn.setImageResource(R.drawable.tab_item_cart_normal);
        tabItemPersonalBtn.setImageResource(R.drawable.tab_item_personal_normal);
        FragmentTransaction ft = fm.beginTransaction();
        if(homeFragment!=null){
            ft.hide(homeFragment);
        }
        if(categoryFragment!=null){
            ft.hide(categoryFragment);
        }
        if(carFragment!=null){
            ft.hide(carFragment);
        }
        if(personalFragment!=null){
            ft.hide(personalFragment);
        }
        switch (id){
            case R.id.tab_item_home:
                tabItemHomeBtn.setImageResource(R.drawable.tab_item_home_focus);
                if(homeFragment==null){
                    homeFragment = new HomeFragment();
                    ft.add(R.id.content,homeFragment).commit();
                }else {
                    ft.show(homeFragment).commit();
                }
                break;
            case R.id.tab_item_category:
                tabItemCategoryBtn.setImageResource(R.drawable.tab_item_category_focus);
                if(categoryFragment==null){
                    categoryFragment = new CategoryFragment();
                    ft.add(R.id.content,categoryFragment).commit();
                }else {
                    ft.show(categoryFragment).commit();
                }
                //categoryFragment.initData();
                break;
            case R.id.tab_item_car:
                MyPresenter.isLogin(new MySubscriber<Boolean>(MyApplication.getContext()) {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        if(!aBoolean){
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivity(intent);
                        }
                    }
                });
                tabItemCarBtn.setImageResource(R.drawable.tab_item_cart_focus);
                if(carFragment==null){
                    carFragment = new CarFragment();
                    ft.add(R.id.content,carFragment).commit();
                }else {
                    ft.show(carFragment).commit();
                }
                break;
            case R.id.tab_item_personal:
                tabItemPersonalBtn.setImageResource(R.drawable.tab_item_personal_focus);
                if(personalFragment==null){
                    personalFragment = new PersonalFragment();
                    ft.add(R.id.content,personalFragment).commit();
                }else {
                    ft.show(personalFragment).commit();
                }
                break;
        }
    }
    @Override
    public void onClick(View view) {
        tabBtn(view.getId());
    }
}